import { BASE_URL } from '../../common/config';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bookName:'',
    bookId:"",
    bookAllComments:[],
    color:'#9e9e9e',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let bookId=options.bookId
    this.setData({
      bookId:bookId
    })
    this.getAllComments(bookId)
  },
  getAllComments(bookId){
    let openid=wx.getStorageSync('openid')
    wx.request({
      url: `${BASE_URL}/AllComments`,
      method:'POST',
      data:{
        bookId:bookId,
        openidUser:openid
      },
      success:(res)=>{
        // console.log(res.data.data)
        this.setData({
          bookAllComments:res.data.data
        })
      },
      fail:(err)=>{}
    })
  },
  toggleLike(e) {
    // console.log(e)
    let index=e.currentTarget.dataset.chance
    let openid=e.currentTarget.dataset.commentid
    // console.log(index)
    let bookAllComments=this.data.bookAllComments
    if(!bookAllComments[index].like_state){
      bookAllComments[index].like=bookAllComments[index].like+1
      this.changeLikes(openid,true)
      this.changeLikeState(openid,true)
    }else{
      bookAllComments[index].like=bookAllComments[index].like-1
      this.changeLikes(openid,false)
      this.changeLikeState(openid,false)
    }
    bookAllComments[index].like_state=!bookAllComments[index].like_state
    this.setData({
      // isLiked: !this.data.isLiked,
      bookAllComments:bookAllComments
    })
    // console.log(bookAllComments)
  },
  changeLikes(openid,state){
    wx.request({
      url: `${BASE_URL}/UpdateCommentList`,
      method:'POST',
      data:{
        bookId:this.data.bookId,
        openidUser:openid,
        state:state
      },
      success:(res)=>{
        // console.log(res)
      },
      fail:(err)=>{}
    })
  },
  changeLikeState(openid,state){
    let openidUser=wx.getStorageSync('openid')
    wx.request({
      url: `${BASE_URL}/UpdateCommentLike`,
      method:'POST',
      data:{
        bookId:this.data.bookId,
        openidUser:openidUser,
        openidComment:openid,
        likeState:state
      },
      success:(res)=>{
        // console.log(res)
      },
      fail:(err)=>{}
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})